﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Kooboo.HtmlFunc;
using Kooboo.HtmlFunc.Providers;
using Kooboo.CMS.SiteConverter.Models;

namespace Kooboo.CMS.SiteConverter.Parser.Razor
{
    public class DetailParser : ParserBase, IParser<IDetail>
    {
        public static Kooboo.CMS.Content.Models.Schema FillSchemas(ParserContext context, string folderName, IList<IDataItem> dataItems)
        {
            return ListParser.FillSchemas(context, folderName, dataItems);
        }

        public static void FillResult(ParserContext context, IDetail obj,
            string engineName, string fieldFormat/*, string linkFormat*/)
        {
            if (obj.StartNode == null) { return; }

            var schema = FillSchemas(context, obj.FolderName, obj.DataItems);

            // where clause
            var whereClause = new Kooboo.CMS.Sites.DataRule.WhereClause();
            whereClause.Operator = Kooboo.CMS.Sites.DataRule.Operator.Equals;
            whereClause.Logical = Kooboo.CMS.Sites.DataRule.Logical.And;
            whereClause.FieldName = "UserKey";
            whereClause.Value1 = "{UserKey}";
            whereClause.Value2 = string.Empty;

            // dataRule
            var folderName = schema.Name;
            var dataRule = new Kooboo.CMS.Sites.DataRule.FolderDataRule();
            dataRule.WhereClauses = new Kooboo.CMS.Sites.DataRule.WhereClause[] { whereClause };
            dataRule.Top = "50";
            dataRule.FolderName = folderName;
            dataRule.CategoryFolderName = string.Empty;

            // dataRuleSetting
            var dataName = schema.Name;
            var dataRuleSetting = new Kooboo.CMS.Sites.Models.DataRuleSetting();
            dataRuleSetting.DataRule = dataRule;
            dataRuleSetting.DataName = dataName;
            dataRuleSetting.TakeOperation = Kooboo.CMS.Sites.Models.TakeOperation.First;

            // do replace
            foreach (var field in obj.DataItems)
            {
                // query the stored column name, or there are some upper-lower-case problems
                var column = schema.Columns.Where(o => o.Name.IgnoreCaseEquals(field.ColumnName)).First();
                var inner = string.Format(fieldFormat, dataName, column.Name/*field.ColumnName*/);
                NodeExtension.ReplaceInner(field.StartNode, field.EndNode, inner);
            }

            // remove func markers
            if (ParserSetting.RemoveFuncMarkers)
            {
                foreach (var item in obj.DataItems)
                {
                    item.EndNode.RemoveWithPrevWhitespace();
                    item.StartNode.ParentNode.RemoveChild(item.StartNode);
                }
            }

            // fill view
            ViewPartParser.FillResult(context, obj, engineName, null, dataRuleSetting);
        }

        #region IParser<IDetail> Members

        public void Parse(IDetail obj)
        {
            var fieldFormat = @"@ViewHelper.EditField(ViewBag.{0}, ""{1}"")"; //var fieldFormat = @"@Html.Raw(ViewBag.{0}.{1})";
            //var linkFormat = @"@Url.FrontUrl().PageUrl(""{0}"", new {{ UserKey = item.UserKey }})";
            FillResult(this.Context, obj, this.EngineName, fieldFormat/*, linkFormat*/);
        }

        #endregion
    }
}
